{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 云片网地址\n",
    "## https://www.yunpian.com\n",
    "\n",
    "## 短信接口api\n",
    "## https://www.yunpian.com/doc/zh_CN/domestic/single_send.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "api_key = '555f6f3935ed99a2a819b8df737e522f'\n",
    "\n",
    "class YunPian:\n",
    "    def __init__(self, api_key):\n",
    "        self.api_key = api_key\n",
    "\n",
    "    def send_single_sms(self, code, mobile):\n",
    "        ## 发送一条短信\n",
    "        url = 'https://sms.yunpian.com/v2/sms/single_send.json'\n",
    "        code = '您的验证码是{}。'.format(code)\n",
    "        res = requests.post(url, data={\n",
    "            'apikey': self.api_key,\n",
    "            'mobile': mobile,\n",
    "            'text': code,\n",
    "        })\n",
    "        return res\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    yun_pian = YunPian(api_key)\n",
    "    res = yun_pian.send_single_sms('1234', 18788889999)\n",
    "    print(res.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## AsyncHttpClient 异步发送短信\n",
    "\n",
    "import json\n",
    "from urllib.parse import urlencode\n",
    "from tornado import httpclient\n",
    "from tornado.httpclient import HTTPRequest\n",
    "\n",
    "class AsyncYunPian:\n",
    "    def __init__(self, api_key):\n",
    "        self.api_key = api_key\n",
    "\n",
    "    async def send_single_sms(self,code, mobile):\n",
    "        http_client = httpclient.AsyncHTTPClient()\n",
    "        url = \"https://sms.yunpian.com/v2/sms/single_send.json\"\n",
    "        text = \"【慕学生鲜】您的验证码是{}。如非本人操作，请忽略本短信\".format(code)\n",
    "        ## urlencode 将字典转成 aa=11&bb=22\n",
    "        post_request = HTTPRequest(url=url, method=\"POST\", body=urlencode({\n",
    "            \"apikey\": self.api_key,\n",
    "            \"mobile\": mobile,\n",
    "            \"text\": text\n",
    "        }))\n",
    "        res = await http_client.fetch(post_request)\n",
    "        return json.loads(res.body.decode(\"utf8\"))\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    import tornado\n",
    "    io_loop = tornado.ioloop.IOLoop.current()\n",
    "\n",
    "    yun_pian = AsyncYunPian(\"d6c4ddbf50ab36611d2f52041a0b949e\")\n",
    "\n",
    "    ## run_sync方法可以在运行完某个协程之后停止事件循环\n",
    "    ## run_sync 通过partial调用对象的方法\n",
    "    from functools import partial\n",
    "    new_func = partial(yun_pian.send_single_sms, \"1234\", \"18785625498\")\n",
    "    io_loop.run_sync(new_func)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
